/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Estacionamento.DAOEspecifico;

import Estacionamento.ClassesBasicas.Cliente;
import Estacionamento.DAOGenerico.DAOGenerico;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author italo.valenca
 */
public class DAOCliente extends DAOGenerico<Cliente> implements IDAOCliente{
    
    EntityManagerFactory emf;
    EntityManager em;

    public DAOCliente() {
        emf = Persistence.createEntityManagerFactory("estacionamento");
        em = emf.createEntityManager();
    }
    
    @Override
    public void ExcluirCliente(Cliente cli) throws Exception {
        try {
            em.getTransaction().begin();
            /*em.remove(produto);*/
            em.remove(em.getReference(Cliente.class, cli.getId()));
            em.getTransaction().commit();
            emf.close();
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());

        }
    }
    
    @Override
    public void AlterarLoginSenha(Cliente cli) throws Exception {
        try {
            em.getTransaction().begin();
            Query query = em.createQuery("UPDATE Cliente set login = :login, senha = :senha  WHERE cpf = :cpfCnpj");
            //query.setParameter("login", cli.getLogin());
            query.setParameter("cpf", cli.getCpfCnpj());
            //query.setParameter("senha", cli.getSenha());
            query.executeUpdate();
            em.getTransaction().commit();
            emf.close();
            /*em.getTransaction().begin();
             Query consulta = em.createQuery("UPDATE Funcionario set login = :login" +
             " and senha = :senha" +
             " where cpf = :cpf" );
             consulta.setParameter("login", fun.getLogin());
             consulta.setParameter("senha", fun.getSenha());
             consulta.setParameter("cpf", fun.getCpf());
             em.getTransaction().commit();
             emf.close();*/

        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
    }
    
    @Override
    public List<Cliente> ListarClientes() throws Exception {

        try {
            em.getTransaction().begin();
            Query consulta = em.createQuery("select cliente from Cliente cliente");
            List<Cliente> cliente = consulta.getResultList();
            em.getTransaction().commit();
            emf.close();
            return cliente;

        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
    }
    
    @Override
    public List<Cliente> ListarClientesPorNome(Cliente cli) throws Exception {

        try {
            em.getTransaction().begin();
            Query consulta = em.createQuery("select cliente from Cliente cliente where cliente.nome = '" + cli.getNome() + "'");
            List<Cliente> cliente = consulta.getResultList();
            em.getTransaction().commit();
            emf.close();
            return cliente;

        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
    }
    /*
    @Override
    public List<Cliente> VerificarLogin(Cliente cli) throws Exception {
        try {
            em.getTransaction().begin();
            Query consulta = em.createQuery("select c from Cliente c WHERE c.login = :login and c.senha = :senha");
            consulta.setParameter("login", cli.getLogin());
            consulta.setParameter("senha", cli.getSenha());
            List<Cliente> clientes = consulta.getResultList();
            em.getTransaction().commit();
            emf.close();
            return clientes;

        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
    }*/
}